package com.candy.candypay.business.callback;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.AlipaySignature;
import com.candy.candypay.config.AliPayConfig;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author suYanLong
 * @version 1.0
 * @date 2024/1/12 17:11
 * @Description 描述
 */
@RestController
@RequestMapping("/alipay/callback")
public class AlipayCallbackController {

    private AliPayConfig aliPayConfig;

    /**
     * "gmt_create" -> "2024-01-11 13:35:14"
     * "charset" -> "UTF-8"
     * "gmt_payment" -> "2024-01-11 13:35:21"
     * "notify_time" -> "2024-01-11 13:35:23"
     * "subject" -> "苹果11"
     * "buyer_id" -> "2088722027905323"
     * "invoice_amount" -> "20.00"
     * "version" -> "1.0"
     * "notify_id" -> "2024011101222133522105320502106651"
     * "fund_bill_list" -> "[{"amount":"20.00","fundChannel":"ALIPAYACCOUNT"}]"
     * "notify_type" -> "trade_status_sync"
     * "out_trade_no" -> "11111"
     * "total_amount" -> "20.00"
     * "trade_status" -> "TRADE_SUCCESS"
     * "trade_no" -> "2024011122001405320502002460"
     * "auth_app_id" -> "9021000133688755"
     * "receipt_amount" -> "20.00"
     * "point_amount" -> "0.00"
     * "buyer_pay_amount" -> "20.00"
     * "app_id" -> "9021000133688755"
     * "seller_id" -> "2088721027932856"
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                // System.out.println(name + " = " + request.getParameter(name));
            }

            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");

            String sign = params.get("sign");
            String content = AlipaySignature.getSignCheckContentV1(params);
            boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign, aliPayConfig.getAlipayPublicKey(), "UTF-8"); // 验证签名
            // 支付宝验签
            if (checkSignature) {
                // 验签通过
                System.out.println("交易名称: " + params.get("subject"));
                System.out.println("交易状态: " + params.get("trade_status"));
                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
                System.out.println("商户订单号: " + params.get("out_trade_no"));
                System.out.println("交易金额: " + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
                System.out.println("买家付款时间: " + params.get("gmt_payment"));
                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));

                // 更新订单未已支付
                System.out.println("已支付");
                System.out.println(tradeNo);
                System.out.println(gmtPayment);
                System.out.println(alipayTradeNo);
                System.out.println(JSONObject.toJSONString(params));
            }
        }
        return "success";
    }

}
